< previous page page_356 next page >

Page 356
First, use plenty of error handling in event procedures. If an error occurs, and you do not handle it with code, the control stops running, bringing down its container as well. Typically, all events that have any code attached to them should be error handled. Some key areas, however, need special attention:
File handling. What if a file does not exist or is write-protected?
Input/Output connectivity. What if the network times out or is unavailable, for example?
Interaction with other controls and the container. Can you successfully use property values from other controls as input to your control?
Boundary conditions. What happens with extreme or incorrect input values (such as, 99999)?
In Chapter 9, ''Visual Basic Programming Fundamentals," you learned about using the On Error statement to control program flow after an error occurs.
sarrow.gifSee "Controlling Program Flow After an Error," p. 220
Adding error handling to your procedures takes only a few lines of code, especially if you create a global error handling routine like the following:
Sub ErrorTrap(err as Integer)
Select Case err

      Case 3021
             sMessage = "Database problem!"
      Case 13
             sMessage = "A System Error has occurred"
' . . . (more error handling code here)

      Case Else
             sMessage = "An unknown Error has occurred"
End Select

End Sub
The ErrorTrap procedure can be called from every event in your control, in the following manner:
1. Use the On Error statement at the beginning of the procedure:
On Error Goto Errhandler
2. In the procedure's Error handler, pass the error number to the ErrorTrap function, as follows:
Errhandler:
ErrorTrap err
Another way to avoid errors is to always use Get and Let procedures for a control's properties, so you can validate property values when they are set. If you create properties by declaring public variables, you have less control over their values. Using property procedures allows you to raise errors immediately if the users try to assign invalid values.

 
< previous page page_356 next page >